'use strict'

const { ApiController } = require('jax')

class Controller extends ApiController {
  get Model() {
    return this.ctx.model.Battle
  }

  // 列表查询前
  beforeIndex() {
    const { ctx } = this
    const { query, helper } = ctx
    const filter = {}

    // 匹配搜索
    ;[''].map((key) => {
      if (query[key] && !query[key] == '') filter[key] = query[key]
    })

    // 关键字模糊查询
    if (query.keyword && !query.keyword == '') filter.title = new RegExp(helper.toMongoRegex(query.keyword, 'i'))

    return {
      filter,
      sort: {
        isTop: 'DESC',
        rank: 'ASC',
      },
    }
  }
}

module.exports = Controller
